import 'package:flutter/material.dart';
import 'package:x3_base_core/x3_base_core.dart';
import 'package:x3_form_core/components/base_search_component.dart';
import 'package:x3_component_core/components/base_components/base_scan.dart';

enum BaseSearchFilterColor {
  dark,
  light,
}

class BaseSearchFilterComponent extends StatefulWidget {
  /// 初始值
  final String value;

  /// 提示文本
  final String hintText;

  final EdgeInsetsGeometry contentPadding;

  /// 高级筛选点击事件
  final VoidCallback onFilterIconPress;

  /// 编辑完成的事件
  final ValueChanged<String> onEditingComplete;

  /// 是否显示扫描图标
  final bool showScanIcon;

  /// 扫描图标的点击事件
  final Function customScanButtonPress;

  /// 把焦点暴露出去
  final void Function(FocusNode focusNode) emitFocusNode;

  /// 背景颜色
  final Color backgroundColor;

  /// 控制器
  final TextEditingController controller;

  /// 内边距
  final EdgeInsetsGeometry padding;

  /// 清除值事件
  final VoidCallback onClearOut;

  /// 颜色
  final BaseSearchFilterColor colorModel;

  /// 输入内容显示的最大行数
  final int maxLines;

  BaseSearchFilterComponent({
    Key key,
    @required this.onEditingComplete,
    this.value,
    this.onFilterIconPress,
    this.hintText = '请输入关键字搜索',
    this.showScanIcon = false,
    this.backgroundColor = Colors.white,
    this.emitFocusNode,
    this.controller,
    this.padding = EdgeInsets.zero,
    this.onClearOut,
    this.colorModel = BaseSearchFilterColor.light,
    this.contentPadding,
    this.maxLines,
    this.customScanButtonPress,
  }) : super(key: key);

  BaseSearchFilterComponent.appBar({
    Key key,
    @required this.onEditingComplete,
    this.value,
    this.onFilterIconPress,
    this.hintText = '请输入关键字搜索',
    this.showScanIcon = false,
    this.backgroundColor = Colors.white,
    this.emitFocusNode,
    this.controller,
    this.padding = const EdgeInsets.symmetric(vertical: 4.0, horizontal: 8.0),
    this.onClearOut,
    this.colorModel = BaseSearchFilterColor.light,
    this.contentPadding,
    this.maxLines,
    this.customScanButtonPress,
  }) : super(key: key);

  @override
  _BaseSearchFilterComponentState createState() => _BaseSearchFilterComponentState();
}

class _BaseSearchFilterComponentState extends State<BaseSearchFilterComponent> {
  /// 输入框控制器
  TextEditingController _controller;

  /// 获取当前的控制器
  TextEditingController get _effectiveController => widget.controller ?? _controller;

  @override
  void initState() {
    if (widget.controller == null) {
      _controller = new TextEditingController();
    }
    super.initState();
  }

  Color get _fillColor => widget.colorModel == BaseSearchFilterColor.light ? Colors.white : BaseBodyTheme.greyBackgroundColor;

  @override
  Widget build(BuildContext context) {
    return Material(
      borderRadius: BorderRadius.circular(2),
      color: widget.backgroundColor,
      child: Container(
        padding: widget.padding,
        child: BaseSearchComponent(
          value: widget.value,
          fillColor: _fillColor,
          hintText: widget.hintText,
          onEditingComplete: widget.onEditingComplete,
          onClearOut: widget.onClearOut,
          controller: _effectiveController,
          emitFocusNode: widget.emitFocusNode,
          contentPadding: widget.contentPadding,
          maxLines: widget.maxLines,
          expansion: Row(
            children: [
              SizedBox(width: 8.0),
              widget.showScanIcon ? _buildScan() : SizedBox(),
              widget.onFilterIconPress != null ? _buildFilterIconWidget() : SizedBox(),
            ],
          ),
        ),
      ),
    );
  }

  /// 扫描组件
  Widget _buildScan() {
    return Container(
      margin: EdgeInsets.only(right: 8.0),
      child: BaseScan(
          customPress: widget.customScanButtonPress,
          callBack: (v) {
            _effectiveController.text = v;
            setState(() {});
            widget.onEditingComplete(_effectiveController.text);
          }),
    );
  }

  /// 构建筛选项图标
  Widget _buildFilterIconWidget() {
    return _buildIconContainer(BaseIconData.x3Filter, () {
      FocusNode().requestFocus();
      widget.onFilterIconPress();
    });
  }

  /// 公用
  Widget _buildIconContainer(IconData icon, GestureTapCallback onTap) {
    return Container(
      margin: EdgeInsets.only(right: 8.0),
      child: InkWell(
        child: Container(alignment: Alignment.center, width: 28, height: 36, child: Icon(icon, color: Colors.grey, size: 18)),
        onTap: onTap,
      ),
    );
  }
}
